package com.pti.usermanager.service.impl;

import com.pti.usermanager.dao.api.IconDao;
import com.pti.usermanager.dao.po.IconPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.IconService;
import com.pti.usermanager.web.dto.request.SaveSortRequest;
import com.pti.usermanager.web.dto.request.UpdateSort;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: IconServiceImpl
 * @Author: liumengyao
 * @Description:
 * @Date: 2019/3/5
 */
@Service
public class IconServiceImpl implements IconService {

    private static final Logger logger = LogManager.getLogger(IconServiceImpl.class);

    @Autowired
    private IconDao iconDao;


    /**
     * @Author liumengyao
     * @Description 查看所有图标
     * @Date 2019/3/5
     */
    @Override
    public ResultResponse findAll(Integer iconTyp) {

        logger.debug("iconTyp:{}",iconTyp);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(iconDao.findAll(iconTyp));
        return resultResponse;
    }



    /**
     * @Author liumengyao
     * @Description 查看分配的图标
     * @Date 2019/3/5
     */
    @Override
    public ResultResponse findBranchData(Long usrId,Integer iconTyp) {

        logger.debug("usrId:{} iconTyp:{}",usrId,iconTyp);

        ResultResponse resultResponse = new ResultResponse();
        List<IconPo> branchData = iconDao.findBranchData(usrId, iconTyp);
        resultResponse.setData(branchData);
        return resultResponse;
    }


    /**
     * @Author liumengyao
     * @Description 左侧图标显示
     * @Date 2019/3/5
     */
    @Override
    public ResultResponse findLeftIconByUsrId(Long usrId,Integer iconTyp) {

        logger.debug("usrId:{} iconTyp:{}",usrId,iconTyp);

        ResultResponse resultResponse = new ResultResponse();
        //查询的为部门  部门类型为1
        Integer orgType = 1;
        Long aLong = iconDao.findorgIdByUsrId(usrId,orgType);
        if(aLong==0){
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }else{
            List<IconPo> iconByOrgId = iconDao.findIconByOrgId(aLong, iconTyp);
            resultResponse.setData(iconByOrgId);
        }
        return resultResponse;
    }


    /**
     * @Author liumengyao
     * @Description 移动上侧与右侧图标
     * @Date 2019/3/6
     */
    @Override
    public ResultResponse moveIcon(UpdateSort updateSort,Long usrId,Integer iconTyp) {

        logger.debug("moveIcon:{} usrId:{} iconTpy:{}",updateSort.getMoveIcon().size(),usrId,iconTyp);

        ResultResponse resultResponse = new ResultResponse();

        try {
            iconDao.delIconByUsrId(usrId,iconTyp);
            for(SaveSortRequest s:updateSort.getMoveIcon()){
                iconDao.moveIcon(s.getUsrId(),s.getIconId(),s.getSort());
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }




}
